package wiki.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.utilities.AlreadyExistingElementException;
import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p>
 * Responsabile dei video
 */
public class VideoChooseServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){String corso=(String) ses.getAttribute("corso");
		String name=req.getParameter("nome");
		name=name.replace(" ", "_");
		name=Setup.getSetup().getMediator().getGpService().changeInputText(name);
		String URL=req.getParameter("url");
		URL=URL.replace("$$||$$", "$$$$$$");
		int ID=Setup.getSetup().getMediator().geteService().insertElement(corso,"video", URL);
		
		String current=(String) ses.getAttribute("current");
		try {
			Setup.getSetup().getMediator().geteService().appendContent(corso,current, name, "video", ID, new ArrayList<String>());
			resp.sendRedirect("./view?page="+current);
		} catch (AlreadyExistingElementException e) {
			resp.sendRedirect("./html/errorpages/nomecontenuto.html"); 
		} catch (PageNotFoundException e) {
			resp.sendRedirect("./html/errorpages/pagina404.html");
		}
		
			} else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else{
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.sendRedirect("./html/errorpages/nopermessi.html");
		
	}
	
}
